package gold.digger;

import gold.utils.InputUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC840 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int numMagicSquaresInside(int[][] grid) {
            int res = 0;
            for (int i = 1; i < grid.length - 1; i++) {
                for (int j = 1; j < grid[0].length - 1; j++) {
                    if (grid[i][j] == 5 && grid[i - 1][j - 1] != 5
                            && grid[i - 1][j] != 5
                            && grid[i - 1][j + 1] != 5
                            && grid[i][j - 1] != 5
                            && grid[i][j + 1] != 5
                            && grid[i + 1][j - 1] != 5
                            && grid[i + 1][j] != 5
                            && grid[i + 1][j + 1] != 5) {
                        int[] record = new int[20];
                        record[5]++;
                        record[grid[i - 1][j - 1]]++;
                        record[grid[i - 1][j]]++;
                        record[grid[i - 1][j + 1]]++;

                        record[grid[i][j - 1]]++;
                        record[grid[i][j + 1]]++;

                        record[grid[i + 1][j - 1]]++;
                        record[grid[i + 1][j]]++;
                        record[grid[i + 1][j + 1]]++;

                        boolean breakEarly = false;
                        for (int k = 1; k <= 9; k++) {
                            if (record[k] != 1) {
                                breakEarly = true;
                                break;
                            }
                        }

                        if (breakEarly) continue;
                        if (grid[i + 1][j] + grid[i - 1][j] != 10) continue;    // 上下
                        if (grid[i][j + 1] + grid[i][j - 1] != 10) continue;
                        if (grid[i - 1][j + 1] + grid[i + 1][j - 1] != 10) continue;
                        if (grid[i - 1][j - 1] + grid[i + 1][j + 1] != 10) continue;

                        if (grid[i - 1][j - 1] + grid[i - 1][j] + grid[i - 1][j + 1] != 15) continue;
                        if (grid[i + 1][j - 1] + grid[i + 1][j] + grid[i + 1][j + 1] != 15) continue;
                        if (grid[i - 1][j - 1] + grid[i][j - 1] + grid[i + 1][j - 1] != 15) continue;
                        if (grid[i - 1][j + 1] + grid[i][j + 1] + grid[i + 1][j + 1] != 15) continue;
                        ++res;
                    }
                }
            }

            return res;
        }
    }

    public void run() {
        Solution solution = new Solution();
//        int[][] arr = {
//                {4, 3, 8, 4},
//                {9, 5, 1, 9},
//                {2, 7, 6, 2}
//        };
//        System.out.println(solution.numMagicSquaresInside(arr));

        int[][] arr = {{1, 8, 6}, {10, 5, 0}, {4, 2, 9}};
        System.out.println(solution.numMagicSquaresInside(arr));

//        int[][] arr = InputUtil.toDoubleIntegerArray("[[4,10,1,6],[2,5,8,5],[9,0,6,4],[1,7,2,9]]");
//        System.out.println(solution.numMagicSquaresInside(arr));

    }

    public static void main(String[] args) throws Exception {
        LC840 an = new LC840();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
